package com.eco.fanliapp.admin.controller.web;


import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.constant.ApiEnum;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppCompany;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IAppCompanyService;
import com.eco.fanliapp.service.ITaobaoSchemeService;
import com.eco.fanliapp.service.TaoBaoPlanService;
import com.eco.fanliapp.utils.MapValueHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
public class TaoBaoPlanController extends BaseController {

    @Autowired
    private IAppCompanyService appCompanyService;

    @Autowired
    private TaoBaoPlanService taoBaoPlanService;

    @Autowired
    private IAppCompanyService companyService;
    @Autowired
    private ITaobaoSchemeService taobaoSchemeService;

    @SysLogAnnotation(value = "获取联盟授权地址",userType = 2)
    @RequestMapping(method = RequestMethod.GET, value = "/web/taobaoplan/getUrl")
    public ServerResponse<String> getUrl() {
        String url = "https://oauth.taobao.com/authorize?"
                + "response_type=token"
                + "&client_id=" + ApiEnum.TAO_CLIENT_ID + ""
                + "&redirect_uri=" + URLEncoder.encode(ApiEnum.TAO_LM_REDIRECT_URI);
            return ServerResponse.createBySuccess("数据获取成功", url);
    }


    @SysLogAnnotation(value = "添加联盟账号",userType = 2)
    @RequestMapping(method = RequestMethod.POST, value = "/web/taobaoplan/add")
    public ServerResponse<String> add(@RequestBody TaobaoScheme taoBaoPlan) {
        taoBaoPlan.setCompanyId(getAuthentication().getCompanyId());
        return taoBaoPlanService.insert(taoBaoPlan);
    }

    @SysLogAnnotation(value = "修改联盟账号",userType = 2)
    @RequestMapping(method = RequestMethod.POST, value = "/web/taobaoplan/update")
    public ServerResponse<String> update(@RequestBody TaobaoScheme taoBaoPlan) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        Long taobaoUserId = taoBaoPlan.getTaobaoUserId();
        //查询是否存在这条数据
        map.put("taobaoUserId",taobaoUserId);
        List<TaobaoScheme> schemeList = taoBaoPlanService.findTaoBaoPlanAll(map);
        if(schemeList.size() >0 ){
            taoBaoPlan.setStatus(1);
            taoBaoPlan.setId(schemeList.get(0).getId());
            ServerResponse<String> response= taoBaoPlanService.update(taoBaoPlan);
            //刷新token
            taobaoSchemeService.selectTaobaoSchemeById(taoBaoPlan.getId(), 0);
            return response;
        }else {
            taoBaoPlan.setCompanyId(getAuthentication().getCompanyId());
            taoBaoPlan.setStatus(1);
            return taoBaoPlanService.insert(taoBaoPlan);
        }
    }

    @SysLogAnnotation(value = "修改联盟账号",userType = 2)
    @RequestMapping(method = RequestMethod.POST, value = "/web/taobaoplan/updateNew")
    public ServerResponse<String> updateNew(String access_token,String taobao_user_nick,Long taobao_user_id) {
        TaobaoScheme taoBaoPlan = new TaobaoScheme();
        taoBaoPlan.setAppSession(access_token);
        taoBaoPlan.setTaobaoName(taobao_user_nick);
        taoBaoPlan.setTaobaoUserId(taobao_user_id);
        HashMap<String,Object> map = new HashMap<String,Object>();
        Long taobaoUserId = taoBaoPlan.getTaobaoUserId();
        //查询是否存在这条数据
        map.put("taobaoUserId",taobaoUserId);
        List<TaobaoScheme> schemeList = taoBaoPlanService.findTaoBaoPlanAll(map);
        if(schemeList.size() >0 ){
            taoBaoPlan.setStatus(1);
            taoBaoPlan.setId(schemeList.get(0).getId());
            ServerResponse<String> response= taoBaoPlanService.update(taoBaoPlan);
            //刷新token
            taobaoSchemeService.selectTaobaoSchemeById(taoBaoPlan.getId(), 0);
            return response;
        }else {
            taoBaoPlan.setCompanyId(getAuthentication().getCompanyId());
            taoBaoPlan.setStatus(1);
            return taoBaoPlanService.insert(taoBaoPlan);
        }
    }
    @SysLogAnnotation(value = "删除联盟账号",userType = 2)
    @RequestMapping(method = RequestMethod.GET, value = "/web/taobaoplan/remove")
    public ServerResponse<String> remove(int id) {
        return taoBaoPlanService.delete(id);
    }

    /**
     * 查询所有的 status = 1 and o.appSession is not null
     */

    @RequestMapping(method = RequestMethod.GET, value = "/web/taobaoplan/find/taobaoplan/status")
    public ServerResponse<PageInfo> findTaoBaoPlanStatus(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<TaobaoScheme> taoBaoPlanStatus = taoBaoPlanService.findTaoBaoPlanStatus();
        return ServerResponse.createBySuccess("查询成功",new PageInfo(taoBaoPlanStatus));
    }
    /**
     * id 查TaoBaoPlan
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/web/taobaoplan/find/taobaoplan/single")
    public TaobaoScheme findTaoBaoPlanById(int id) {
        return taoBaoPlanService.findTaoBaoPlanById(id);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/web/taobaoplan/find/taobaoplan/all")
    public ServerResponse<PageInfo> findTaoBaoPlanAll(@RequestParam HashMap<String, Object> params) {

        Integer companyId = getAuthentication().getCompanyId();

        if(companyId == 1){
            companyId = null;
        }
        params.put("companyId",companyId);

        MapValueHelper mapValueHelper = new MapValueHelper(params);
        int page = mapValueHelper.getInt("page");
        int size = mapValueHelper.getInt("size");
        PageHelper.startPage(page,size);
        List<TaobaoScheme> taoBaoPlanAll = taoBaoPlanService.findTaoBaoPlanAll(params);
        for(TaobaoScheme taoBaoPlan :taoBaoPlanAll){
            long up_time = taoBaoPlan.getExpireTime().getTime();
            if(up_time < System.currentTimeMillis()){
                taoBaoPlan.setStatus(0);
                taoBaoPlanService.update(taoBaoPlan);
            }
        }
        return ServerResponse.createBySuccess(companyId==null?"1":"查询成功",new PageInfo(taoBaoPlanAll));
    }

    public static void main(String args[]) {
        String str = "http%3a%2f%2fconsole.shengyaapp.com%2Fcallback%2Ftaobao";
        String str2 = "http://console.shengyaapp.com/callback/taobao";
        log.info(URLEncoder.encode(str));

    }
}
